﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemoEF.Models;
using PagedList;
using PagedList.Mvc;

namespace MVCDemoEF.Controllers
{
    public class SearchController : Controller
    {
        private EmployeeExContext db = new EmployeeExContext();

        //
        // GET: /Search/

        public ActionResult Index(string searchBy, string search, int? page, string sortBy)
        {
            ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Name desc" : "";
            ViewBag.SortGenderParameter = sortBy == "Gender" ? "Gender desc" : "Gender";

            var employees = db.EmployeeExes.AsQueryable();

            if (searchBy == "Gender")
                employees = employees.Where(x => x.Gender.ToUpper().Equals(search.ToUpper()) || search == null);
            else
                employees = employees.Where(x => x.FullName.StartsWith(search) || search == null);

            switch (sortBy)
            {
                case "Name desc":
                    employees = employees.OrderByDescending(x => x.FullName);
                    break;
                case "Gender desc":
                    employees = employees.OrderByDescending(x => x.Gender);
                    break;
                case "Gender":
                    employees = employees.OrderBy(x => x.Gender);
                    break;
                default:
                    employees = employees.OrderBy(x => x.FullName);
                    break;
            }

            return View(employees.ToPagedList(page ?? 1, 2));
        }

        //
        // GET: /Search/Details/5

        public ActionResult Details(int id = 0)
        {
            EmployeeEx employeeex = db.EmployeeExes.Single(e => e.Id == id);
            if (employeeex == null)
            {
                return HttpNotFound();
            }
            return View(employeeex);
        }

        //
        // GET: /Search/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Search/Create

        [HttpPost]
        public ActionResult Create(EmployeeEx employeeex)
        {
            if (ModelState.IsValid)
            {
                db.EmployeeExes.AddObject(employeeex);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employeeex);
        }

        //
        // GET: /Search/Edit/5

        public ActionResult Edit(int id = 0)
        {
            EmployeeEx employeeex = db.EmployeeExes.Single(e => e.Id == id);
            if (employeeex == null)
            {
                return HttpNotFound();
            }
            return View(employeeex);
        }

        //
        // POST: /Search/Edit/5

        [HttpPost]
        public ActionResult Edit(EmployeeEx employeeex)
        {
            if (ModelState.IsValid)
            {
                db.EmployeeExes.Attach(employeeex);
                db.ObjectStateManager.ChangeObjectState(employeeex, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employeeex);
        }

        //
        // GET: /Search/Delete/5

        public ActionResult Delete(int id = 0)
        {
            EmployeeEx employeeex = db.EmployeeExes.Single(e => e.Id == id);
            if (employeeex == null)
            {
                return HttpNotFound();
            }
            return View(employeeex);
        }

        //
        // POST: /Search/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            EmployeeEx employeeex = db.EmployeeExes.Single(e => e.Id == id);
            db.EmployeeExes.DeleteObject(employeeex);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}